﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Garden.Func;
using Garden.Model;
using System.Xml.Linq;
namespace Garden
{
    public class GardenConfig
    {
        private static XDocument _configFile;
        //静态构造
        static GardenConfig()
        {
            CheckConfigFile();
            _configFile = XDocument.Load(Utility.GARDENCONFIGFILENAME);
        }

        private static void CheckConfigFile()
        {
            FileInfo f = new FileInfo(Utility.GARDENCONFIGFILENAME);
            if (!f.Exists)
            {
                StreamWriter sw = new StreamWriter(f.Create());
                sw.Write(Utility.GARDENCONFIGFILETEMPLET);
                sw.Close();
            }
        }

        public static List<Seed> StealSeeds
        {
            get
            {
                XElement stealEl = _configFile.Root.Element("steal");
                var list = from el in stealEl.Elements() select el;
                List<Seed> ret = new List<Seed>();
                foreach (XElement element in list)
                {
                    ret.Add(new Seed { ID = element.Value });
                }
                return ret;
            }
            set
            {
                XElement stealEl = _configFile.Root.Element("steal");
                stealEl.RemoveNodes();
                foreach (var seed in value)
                {
                    XElement el = new XElement("seed", seed.ID);
                    stealEl.Add(el);
                }
                _configFile.Save(Utility.GARDENCONFIGFILENAME);
            }
        }

        public static bool IsStealSeed(Seed seed)
        {
            return StealSeeds.Exists(s => s.ID == seed.ID);
        }
        public static bool AutoSow
        {
            get { return _configFile.Root.Element("autoSow").Value == "1"; }
            set
            {
                _configFile.Root.Element("autoSow").Value = value ? "1" : "0";
                _configFile.Save(Utility.GARDENCONFIGFILENAME);
            }
        }
        public static int SowID
        {
            get { return int.Parse(_configFile.Root.Element("sowid").Value); }
            set
            {
                _configFile.Root.Element("sowid").Value = value.ToString();
                _configFile.Save(Utility.GARDENCONFIGFILENAME);
            }
        }
        public static bool Water
        {
            get { return _configFile.Root.Element("water").Value == "1"; }
            set
            {
                _configFile.Root.Element("water").Value = value ? "1" : "0";
                _configFile.Save(Utility.GARDENCONFIGFILENAME);
            }
        }
        public static bool Vermin
        {
            get { return _configFile.Root.Element("vermin").Value == "1"; }
            set
            {
                _configFile.Root.Element("vermin").Value = value ? "1" : "0";
                _configFile.Save(Utility.GARDENCONFIGFILENAME);
            }
        }
        public static bool Grass
        {
            get { return _configFile.Root.Element("grass").Value == "1"; }
            set
            {
                _configFile.Root.Element("grass").Value = value ? "1" : "0";
                _configFile.Save(Utility.GARDENCONFIGFILENAME);
            }
        }
    }
}
